package com.example.dragdemo;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;

import com.example.dragdemo.customerview.CustomerGridView;
import com.example.dragdemo.customerview.DataInfo;
import com.example.dragdemo.customerview.DragLayer;
import com.example.dragdemo.customerview.FolderDrop;
import com.example.dragdemo.customerview.FolderDrop.FloderDropListener;
import com.example.dragdemo.draganddrop.OnDropCallBack;

public class MainActivity extends Activity {

	private CustomerGridView mGridView;
	private String LOG_TAG = "MainActivity";
	private List<File> mFiles;
	private GridAdapter mGridAdapter;
	private File parentFile;
	private FolderDrop mFolderDrop;
	private DragLayer mDragLayer;
	private String testFile = "DragDemoTestFile";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.myuploadfile);
		initDatas();
		initLayout();
	}

	private void initDatas() {
		if (isExternalStorageReadable()) {
			parentFile = Environment.getExternalStorageDirectory();
			parentFile = new File(parentFile,testFile);
			if (!parentFile.exists()) {
				parentFile.mkdirs();
			}
			if (parentFile.isDirectory()) {
				mFiles = Arrays.asList(parentFile.listFiles());
			}
		}
	}

	
	private void initLayout() {
		mGridView = (CustomerGridView) findViewById(R.id.grid_view);
		mDragLayer = (DragLayer) findViewById(R.id.myuploadfile_page);
		mFolderDrop = (FolderDrop) findViewById(R.id.folder_drop);
		
		mFolderDrop.setFloderDropListener(new mFloderDropListener());
		mFolderDrop.setVisibility(View.GONE);
		
		mGridAdapter = new GridAdapter(this, mFiles);
		mGridView.setAdapter(mGridAdapter);
		mGridView.setOnItemClickListener(new mOnItemClickListener());
		mGridView.setOnItemLongClickListener(new mOnItemLongClickListener());
		
		mGridView.setDragger(mDragLayer);
		mGridView.setOnDropListener(new  OnDropCallBack(){   
			public  void  onDropCompleted(DataInfo sourceInfo,DataInfo destInfo) {              
				mFolderDrop.setVisibility(View.GONE);
				if (destInfo == null || sourceInfo == null) {
					return ;
				}
				if(destInfo==sourceInfo) return ;
				Map<String, Object> retmap=null;
		/*			if((destInfo.GetIsDir() != null) && (destInfo.GetIsDir().equals("1"))){
						retmap = libCloud.Move_file(sourceInfo.getId(), destInfo.getId(), sourceInfo.getName());
						if(retmap!=null && retmap.get("code").equals("1")){
							MyUploadFileDataAdapter adapter1 = (MyUploadFileDataAdapter) ((CustomerGridView) mListViews.get(currentPage -1)).getAdapter();
							adapter1.getList().remove(sourceInfo.getPosition());
							adapter1.notifyDataSetChanged();																
							if(docList!=null){
								docList.clear();
							}
							docList = null;
							GetDataTask gdt = new GetDataTask();
							gdt.execute(currentPage,"");
						}
					}*/
			}   
		}); 
	}
	
	public void newFile(View v){
		if(!parentFile.getAbsolutePath().contains(testFile)){
			Toast.makeText(this, "不得在"+testFile+"测试文件夹以为创建文件", Toast.LENGTH_SHORT).show();
			return;
		}
		int index = 0;
	   	switch (v.getId()) {
		case R.id.new_file:
			File ff = new File(parentFile,"文件夹"+(++index));
			while(ff.exists())ff = new File(parentFile,"文件夹"+(++index));
    		ff.mkdirs();
			break;
		case R.id.new_text:
			File ft = new File(parentFile,"文件"+(++index)+".txt");
		       try{
		         while(ft.exists())ft = new File(parentFile,"文件"+(++index)+".txt");
		        ft.createNewFile();
		        }catch(IOException e){
		            return;
		        }
			break;
		}
	   	mFiles = Arrays.asList(parentFile.listFiles());
	   	mGridAdapter.setDatas(mFiles);
	   	mGridAdapter.notifyDataSetChanged();
	}
	
	class mFloderDropListener implements FloderDropListener {
		public void onDropOutFoder(Object dragInfo) {
			// System.out.println("=============================drop outside of folder ==================================");
			mFolderDrop.setVisibility(View.GONE);
	/*		if (dragInfo != null) {
				DataInfo info = (DataInfo) dragInfo;
				Map<String, Object> retmap = null;
				mGridAdapter.getList().remove(info.getPosition());
				mGridAdapter.notifyDataSetChanged();
			}*/
		}
	}

	class mOnItemClickListener implements
			android.widget.AdapterView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			File f = mFiles.get(position);
			if (f.isDirectory()) {
				mFiles = Arrays.asList(f.listFiles());
				mGridAdapter.setDatas(mFiles);
				mGridAdapter.notifyDataSetChanged();
				parentFile = f;
			}
		}
	}

	class mOnItemLongClickListener implements OnItemLongClickListener {
		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View view,
				int position, long id) {
			mFolderDrop.setVisibility(View.VISIBLE);
			mGridView.startDrag(view);
			return true;
		}
	}

	@Override
	public void onBackPressed() {
		if (!parentFile.equals(Environment.getExternalStorageDirectory())) {
			parentFile = parentFile.getParentFile();
			mFiles = Arrays.asList(parentFile.listFiles());
			mGridAdapter.setDatas(mFiles);
			mGridAdapter.notifyDataSetChanged();
		} else {
			super.onBackPressed();
		}
	}


	public boolean isExternalStorageWritable() {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			return true;
		}
		return false;
	}

	public boolean isExternalStorageReadable() {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)
				|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			return true;
		}
		return false;
	}

}
